<!DOCTYPE html>
<html xmlns:wb="http://open.weibo.com/wb">

<head>
	<meta charset="UTF-8"></meta>>
	<title>微信登录</title>
</head>

<body>
<script src="//tjs.sjs.sinajs.cn/open/api/js/wb.js?appkey=3047603970" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
    //<![CDATA[
	var GWC = {
		version: '1.1.1',
		urlParams: {},
		appendParams: function(url, params) {
		    console.log("url="+url)
			if (params) {
				var baseWithSearch = url.split('#')[0];
				var hash = url.split('#')[1];
				for (var key in params) {
					var attrValue = params[key];
					if (attrValue !== undefined) {
						var newParam = key + "=" + attrValue;
						if (baseWithSearch.indexOf('?') > 0) {
							var oldParamReg = new RegExp('^' + key + '=[-%.!~*\'\(\)\\w]*', 'g');
							if (oldParamReg.test(baseWithSearch)) {
								baseWithSearch = baseWithSearch.replace(oldParamReg, newParam);
							} else {
								baseWithSearch += "&" + newParam;
							}
						} else {
							baseWithSearch += "?" + newParam;
						}
					}
				}
				
				if (hash) {
					url = baseWithSearch + '#' + hash;
				} else {
					url = baseWithSearch;
				}
			}
			return url;
		},
		getUrlParams: function() {
			var pairs = location.search.substring(1).split('&');
			for (var i = 0; i < pairs.length; i++) {
				var pos = pairs[i].indexOf('=');
				if (pos === -1) {
					continue;
				}
				GWC.urlParams[pairs[i].substring(0, pos)] = decodeURIComponent(pairs[i].substring(pos + 1));
			}
		},
		doRedirect: function() {
			var code = GWC.urlParams['code'];
			var clientId = GWC.urlParams['appid'];
			var client_secret = GWC.urlParams['client_secret'];
			var accesstoken = GWC.urlParams['accesstoken'];
			console.log(GWC)
			var redirectUri;
			if (!code||code=='undefined') {
			    alert("获取code")
				//第一步，没有拿到code，跳转至微博授权页面获取code
				redirectUri = GWC.appendParams('https://api.weibo.com/oauth2/authorize', {
					'client_id': clientId,
					'redirect_uri': encodeURIComponent(location.href),
					'response_type': 'code',
				});
                location.href = "https://api.weibo.com/oauth2/authorize?client_id=3047603970&redirect_uri=http://saqw.cn/jumpweibo.html&response_type=code";
			} else  if(code&&code!='undefined'){
				//第二步，从微博授权页面跳转回来，已经获取到了code，通过code获取access_token
                alert("发送post获取accesstoken");
//                $.ajax({
//                    type: "POST",
//                    async:false,
//                    url:  "https://api.weibo.com/oauth2/access_token",
//                    data: {"code":code,"client_id":clientId,"client_secret":client_secret,"redirect_uri":"http://myqpp.tunnel.qydev.com/jumpcode.html","grant_type":"authorization_code"},
//                    dataType: "json",
//                    success: function (data) {
//                        console.log(data)
//                        accesstoken = JSON.stringify(data);
//                        location.href = "http://wj.tunnel.qydev.com/tokens/weiboinfo?accesstoken="+accesstoken+"&platform=PCH5";
//                    }
//                });

                WB2.anyWhere(function(W){
                    //数据交互
                    W.parseCMD('https://api.weibo.com/oauth2/access_token', function(oResult, bStatus) {
                        if(bStatus) {
                            //to do something...
							alert("!23")
                        }
                    }, {
                        screen_name : '姚晨'
                    }, {
                        method : 'post',
                        cache_time : 30
                    });
                });
			}
		}
	};
	
	GWC.getUrlParams();
	GWC.doRedirect();
    //]]>
</script>
</body>

</html>